#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 110;
const int INF = 200000000;
int w[maxn][maxn];
int dist[maxn];
bool vis[maxn];
int level[maxn], value[maxn];
bool can_change[maxn];
int limit_level, n;
int Dijkstra() {
  int mini_cost = INF;
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; ++i) {
    dist[i] = INF;
  }
  dist[1] = 0;
  for (int i = 1; i <= n; ++i) {
    int x, m = INF;
    for (int y = 1; y <= n; ++y) {
      if (!vis[y] && dist[y] <= m && can_change[y]) m = dist[x = y];
    }
    vis[x] = true;

    for (int y = 1; y <= n; ++y) {
      if (can_change[y]) dist[y] = min(dist[y], dist[x] + w[x][y]);
    }
  }
  for (int y = 1; y <= n; ++y) {
    dist[y] += value[y];
    mini_cost = min(mini_cost, dist[y]);
  }
  return mini_cost;
}
int main() {
  scanf("%d%d", &limit_level, &n);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (i == j)
        w[i][j] = 0;
      else
        w[i][j] = INF;
    }
  }
  for (int i = 1; i <= n; ++i) {
    int change;
    scanf("%d%d%d", &value[i], &level[i], &change);
    for (int j = 1; j <= change; ++j) {
      int y, Value;
      scanf("%d%d", &y, &Value);
      w[i][y] = Value;
    }
  }
  int King_level = level[1];
  int m, minicost = INF;
  // 此题的关键在于等级限制的处理，最好的办法是采用枚举，
  // 即假设酋长等级为5，等级限制为2，那么需要枚举等级从3~5,4~6,5~7
  for (int i = 0; i <= limit_level; ++i) {
    memset(can_change, false, sizeof(can_change));
    for (int j = 1; j <= n; ++j) {
      if (level[j] >= King_level - limit_level + i &&
          level[j] <= King_level + i)
        can_change[j] = true;
      minicost = min(minicost, Dijkstra());
    }
  }
  printf("%d\n", minicost);
}
